#pragma once

#include "ReverseIterator.h"

namespace Free
{
	template <class T>
	class vector
	{
	public:

		typedef T* iterator;
		typedef const T* const_iterator;

		typedef ReverseIterator<iterator, T&, T*> reverse_iterator;
		typedef ReverseIterator<const_iterator, const T&, const T*> const_reverse_iterator;

		reverse_iterator rbegin()
		{
			return reverse_iterator(end());
		}
		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}

		vector()
			:_start(nullptr)
			,_finish(nullptr)
			,_end_of_storage(nullptr)
		{}

		~vector()
		{
			delete[] _start;
			_start = _finish = _end_of_storage = nullptr;
		}

		vector(const vector<T>& v)
		{
			reserve(capacity());
			for (auto& e : v)
			{
				push_back(e);
			}
		}

		//vector<int> v1 = {1,2,3,4,5}
		vector(initializer_list<T> il)
		{
			reserve(il.size());
			for (auto& e : il)
			{
				push_back(e);
			}
		}

		template<class InputIterator>
		vector(InputIterator first,InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}

		vector(size_t n, const vector<T>& val = T())
		{
			reserve(n);
			for (int i = 0;i < n;i++)
			{
				push_back(val);
			}
		}
		//防止和Inputiteratot冲突
		vector(int n, const vector<T>& val = T())
		{
			reserve(n);
			for (int i = 0;i < n;i++)
			{
				push_back(val);
			}
		}

		vector<int>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}

		void swap(vector<T>& v)
		{
			std::swap(_start,v._start);
			std::swap(_finish,v._finish);
			std::swap(_end_of_storage,v._end_of_storage);
		}

	

		void push_back(const T& val)
		{
			//判断扩容
			//if (_finish == _end_of_storage)
			//{
			//	size_t old_size = size();
			//	size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
			//	T* tmp = new T[newcapacity];
			//	memcpy(tmp, _start, sizeof(T) * size());
			//	delete[] _start;
			//	_start = tmp;
			//	_finish = tmp + old_size;//容易出错
			//	_end_of_storage = tmp + newcapacity;
			//}
			//*_finish = val;
			//_finish++;

			insert(end(), val);
		}

		void pop_back()
		{
			assert(!empty());
			_finish--;
		}

		void reserve(size_t n)
		{
			if (capacity() < n)
			{
				size_t old_size = size();
				T* tmp = new T[n];
				//memcpy(tmp, _start, sizeof(T) * size());
				for (int i = 0; i < old_size; i++)
				{
					tmp[i] = _start[i];
				}
				delete[] _start;
				_start = tmp;
				_finish = tmp + old_size;
				_end_of_storage = tmp + n;
			}
		}

		void resize(size_t n, const T& val = T())
		{
			if (size() < n)
			{
				//扩容
				reserve(n);
				while (_finish < _start + n)
				{
					*_finish = val;
					_finish++;
				}
			}
			else
			{
				//删减
				_finish = _start + n;
			}
		}

		void insert(iterator pos, const T& val)
		{
			assert(pos <= _finish);
			assert(pos >= _start);

			if (_finish == _end_of_storage)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : 2 * capacity());
				pos = len + _start;//易错点
			}
			iterator it1 = _finish - 1;
			while (it1 >= pos)
			{
				*(it1 + 1) = *it1;
				it1--;
			}
			*pos = val;
			_finish++;
		}

		iterator erase(iterator pos)
		{
			assert(pos < _finish);
			assert(pos >= _start);
			iterator it1 = pos + 1;
			while (it1 < _finish)
			{
				*(it1 - 1) = *it1;
				it1++;
			}
			_finish--;
			return pos;
		}

		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}

		const_iterator begin()const
		{
			return _start;
		}
		const_iterator end()const
		{
			return _finish;
		}
		size_t size()
		{
			return _finish - _start;
		}
		size_t capacity()
		{
			return _end_of_storage - _start;
		}
		T& operator[](size_t pos)
		{
			assert(pos <= _finish);
			return *(_start + pos);
		}
		const T& operator[](size_t pos)const
		{
			assert(pos <= _finish);
			return *(_start + pos);
		}
		bool empty()
		{
			return _start == _finish;
		}

	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _end_of_storage = nullptr;
	};

	template <class T>
	void Print_vector(const T& v)
	{
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;
		//typename 告诉编译器这是个类型
		//typename vector<T>::const_iterator it = v.begin();
		//while (it != v.end())
		//{
		//	cout << *it << " ";
		//	it++;
		//}
		//for (int i = 0;i < size();i++)
		//{
		//	cout << v[i] << " ";
		//}
	}


	void Test1_vector()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);
		Print_vector(v1);
		cout << endl;
		v1.reserve(100);
		cout << v1.capacity() << ' ' << v1.size();
		v1.resize(2);
		cout << v1.capacity() << ' ' << v1.size() << endl;
		Print_vector(v1);
		v1.resize(200);
		Print_vector(v1);
	}

	void Test2_vector()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);
		v1.insert(v1.begin(),3);
		Print_vector(v1);
	}

	void Test3_vector()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);

		vector<int> v2(v1);//拷贝
		
		vector<int> v3 = { 1,2,3,4,5,6 };
		Print_vector(v2);
		Print_vector(v3);

		vector<int> v4 = v1;
		Print_vector(v4);
	}

	void Test4_vector()
	{
		//有关initializer_list
		int a = 1;
		int b = { 1 };
		int c{ 1 };
		//上面写法均正确 但是不推荐后面两种
		vector<int> v1 = { 1,2,3,4,5,6 };//隐式类型转化
		vector<int> v2({ 1,2,3,4,5,6,7,8 });//直接构造
		Print_vector(v1);
		Print_vector(v2);
	}

	void Test5_vector()
	{
		vector<string> v;
		v.push_back("11111");
		v.push_back("22222");
		v.push_back("33333");
		v.push_back("44444");
		v.push_back("55555");
		for (auto& e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	//有关迭代器失效
	void Test6_vector()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);
		v1.push_back(5);
		v1.push_back(6);
		v1.push_back(7);
		v1.push_back(8);
		Print_vector(v1);
		// insert以后，it就失效了，不要使用了
		vector<int>::iterator it = v1.begin() + 3;
		v1.insert(it, 40);
		Print_vector(v1);
		//it = v1.begin() + 3;
		cout << *it << endl;
	}

	void Test7_vector()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);
		v1.push_back(4);
		v1.push_back(5);
		v1.push_back(4);
		// 删除偶数 -- 迭代器失效以后，不要直接使用，如果要使用按规则重新更新后使用
		vector<int>::iterator it = v1.begin();
		while (it != v1.end())
		{
			if (*it % 2 == 0)
			{
				it = v1.erase(it);//解决方法
			}
			else
			{
				++it;
			}
		}
		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;
	}
	void Test8_vector()
	{
		vector<int> v1({ 1, 2, 3 });
		vector<int> v2;
		v2 = v1;
	}

}

